/**
 * Created by ghh on 2017/2/20.
 */
// 全局对象
function Ship(ctx) {
    gameMonitor.im.loadImage(["static/img/player.png"]);
    this.width = 80;
    this.height = 80;
    this.left = gameMonitor.w / 2 - this.width/2;
    this.top = gameMonitor.h - 2*this.height;
    this.player = gameMonitor.im.createImage("static/img/player.png");

    this.paint = function () {
        ctx.drawImage(this.player, this.left, this.top, this.width, this.height);
    };
    // 修改飞船的left和top值，防止溢出频幕，每次移动后调用paint方法来重新绘制飞船。
    this.setPosition = function (event) {
        this.left = event.changedTouches[0].clientX - this.width/2 -16;
        this.top = event.changedTouches[0].clientY - this.height/2;
        if(this.left < 0){
            this.left = 0;
        } else if(this.left > 320-this.width){
            this.left = 320 - this.width;
        }
        if(this.top < 0){
            this.top = 0;
        } else if(this.top > gameMonitor.h - this.height){
            this.top = gameMonitor.h - this.height;
        }
        this.paint();
    };
    //坚挺touch事件，计算新的位置
    this.controll = function () {
        var _this = this;
        var stage = $("#gamepanel");
        var currentX = this.left,
            currentY = this.top,
            move = false;
        stage.on("touchstart", function (event) {
            _this.setPosition(event);
            move = true;
        }).on("touchend", function (event) {
            move = false;
        }).on("touchmove", function (event) {
            event.preventDefault();
            _this.setPosition(event);
        });

    }
}

// 月饼
function Food(type, left, id) {
    this.speedUpTime = 300;
    this.id = id;
    this.type = type;
    this.width = 50;
    this.height = 50;
    this.left = left;
    this.top = -50;
    this.speed = 0.04 * Math.pow(1.2, Math.floor(gameMonitor.time/this.speedUpTime));
    this.loop = 0;

    var p = this.type == 0 ? "static/img/food1.png" : "static/img/food2.png";
    this.pic = gameMonitor.im.createImage(p);
}
Food.prototype.paint = function (ctx) {
    ctx.drawIamge(this.pic, this.left, this.top, this.width, this.height);
}
Food.prototype.move = function () {
    
}
function ImageMonitor() {
    var imgArray = [];
    return {
        createImage: function (src) {
            return typeof imgArray[src] != "undefined" ? imgArray[src] : (imgArray[src] = new Image(), imgArray[src].src = src)
        },
        loadImage: function (arr, callback) {
            for (var i=0, l=arr.length; i<1; i++){
                var img = arr[i];
                imgArray[img] = new Image();
                imgArray[img].onload = function () {
                    if (i == l-1 && typeof callback == "function"){
                        callback();
                    }
                }
                imgArray[img].src = img;
            }
        }
    }
}

var gameMonitor = {
    w: 320,
    h: 568,
    bgWidth: 320,   //背景图宽度
    bgHeight: 1126,   //背景图长度
    time: 0,
    timer: null,
    bgSpeed: 2,
    bgLoop: 0,
    bgDistance: 0,  //背景位置



    // 背景滚动
    rollBg: function (ctx) {
        if(this.bgDistance >= this.bgHeight){
            this.bgLoop = 0;
        }
        this.bgDistance = ++this.bgLoop * this.bgSpeed;
        ctx.drawImage(this.bg, 0, this.bgDistance-this.bgHeight, this.bgWidth, this.bgHeight);
        ctx.drawImage(this.bg, 0, this.bgDistance, this.bgWidth, this.bgHeight);
    }
}